package com.hilatest.osgi.test;

import java.util.HashMap;
import java.util.Map;

import org.apache.felix.framework.Felix;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;

public class BundleContainer {

	private Felix framework;
	
	@Before
	public void initial() throws BundleException{
		final Map<String, String> configMap = new HashMap<String, String>();
	      configMap.put(Constants.FRAMEWORK_STORAGE_CLEAN, "onFirstInit");
	      framework = new Felix(configMap);
	      framework.init();
	      framework.start();
	}

	@Test
	public void testHelloworld() throws BundleException{
		final BundleContext context = framework.getBundleContext();
		
		Bundle provider = context.installBundle("file:bundles/provider-3.0.jar");
	    Bundle consumer = context.installBundle("file:bundles/consumer-3.0.jar");

	    provider.start();
	    consumer.start();
	    consumer.stop();
	    provider.stop();
	}
	
	@After
	public void clearup() throws BundleException{
		framework.stop();
	}

	/**
	 * @return the framework
	 */
	public Felix getFramework() {
		return framework;
	}

	/**
	 * @param framework the framework to set
	 */
	public void setFramework(Felix framework) {
		this.framework = framework;
	}

	protected BundleContext getContext(){
		return framework.getBundleContext();
	}
	
	
}
